package com.dense.kuiniu.bookborrow.receiver;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;

import java.util.Locale;

/**
 * @author Administrator
 * @date 2021/1/25
 */
public class SystemTTS extends UtteranceProgressListener implements TTS, TextToSpeech.OnUtteranceCompletedListener {
    private TextToSpeech textToSpeech; // 系统语音播报类
    private boolean isSuccess = true;

    private SystemTTS() {
    }

    private static final class Holder {
        private static final SystemTTS INSTANCE = new SystemTTS();
    }

    public static SystemTTS getInstance() {
        return Holder.INSTANCE;
    }

    public void init(Context context) {
        textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int i) {
                //系统语音初始化成功
                if (i == TextToSpeech.SUCCESS) {
                    int result = textToSpeech.setLanguage(Locale.CHINA);
                    textToSpeech.setPitch(0.5f);// 设置音调，值越大声音越尖（女生），值越小则变成男声,1.0是常规
                    textToSpeech.setSpeechRate(1.2f);
                    textToSpeech.setOnUtteranceProgressListener(SystemTTS.this);
                    textToSpeech.setOnUtteranceCompletedListener(SystemTTS.this);
                    if (result == TextToSpeech.LANG_MISSING_DATA
                            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        //系统不支持中文播报
                        isSuccess = false;
                    }
                }
            }
        });
    }


    @Override
    public void onUtteranceCompleted(String utteranceId) {

    }

    @Override
    public void onStart(String utteranceId) {

    }

    @Override
    public void onDone(String utteranceId) {

    }

    @Override
    public void onError(String utteranceId) {

    }

    @Override
    public void playText(String playText) {
        if (textToSpeech != null) {
            textToSpeech.speak(playText, TextToSpeech.QUEUE_FLUSH, null);
        }
    }

    @Override
    public void stopSpeak() {

    }
}